package com.cms.stu.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cms.stu.dao.ClassDao;
import com.cms.stu.dao.DepDao;
import com.cms.stu.dao.MajorDao;
import com.cms.stu.entity.ClassEntity;
import com.cms.stu.entity.DepEntity;
import com.cms.stu.entity.MajorEntity;
import com.cms.stu.entity.vo.StudentAllVo;
import com.cms.stu.to.StudentDetailTo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cms.common.utils.PageUtils;
import com.cms.common.utils.Query;

import com.cms.stu.dao.StudentDao;
import com.cms.stu.entity.StudentEntity;
import com.cms.stu.service.StudentService;

import javax.annotation.Resource;


@Service("studentService")
public class StudentServiceImpl extends ServiceImpl<StudentDao, StudentEntity> implements StudentService {

    @Resource
    private MajorDao majorDao;

    @Resource
    private ClassDao classDao;

    @Resource
    private DepDao depDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        QueryWrapper<StudentEntity> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(key)){
            wrapper.like("name", key).or().eq("id", key).or().eq("class_id", key);
        }
        IPage<StudentEntity> page = this.page(
                new Query<StudentEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void autoSave(StudentEntity student) {
        Long classId = student.getClassId();
        Integer count = baseMapper.selectCount(new QueryWrapper<StudentEntity>().eq("class_id", classId)) + 1;
        ClassEntity classEntity = classDao.selectById(classId);
        Long majorId = classEntity.getMajorId();
        MajorEntity majorEntity = majorDao.selectById(majorId);
        System.out.println((count > 9 ? count : "0" + count));
        String code = classEntity.getLevel() + majorEntity.getCode() + classEntity.getCode() + (count > 9 ? count : "0" + count);
        student.setNum(code);
        baseMapper.insert(student);

        DepEntity depEntity = depDao.selectById(majorEntity.getDepId());
        depEntity.setCount(depEntity.getCount() + 1);
        depDao.updateById(depEntity);

        majorEntity.setCount(majorEntity.getCount() + 1);
        majorDao.updateById(majorEntity);

        classEntity.setCount(classEntity.getCount() + 1);
        classDao.updateById(classEntity);

    }

    /**
     * 登录
     * @param studentEntity 登录实体类
     * @return 用户的信息
     */
    @Override
    public StudentEntity login(StudentEntity studentEntity) {
        QueryWrapper<StudentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("num", studentEntity.getNum()).eq("password", studentEntity.getPassword());
        StudentEntity entity = baseMapper.selectOne(wrapper);
        return entity;
    }

    @Override
    public StudentEntity selectInfoById(Long id) {
        StudentEntity student = baseMapper.selectById(id);
        Long classId = student.getClassId();
        ClassEntity classEntity = classDao.selectById(classId);
        Long majorId = classEntity.getMajorId();
        MajorEntity majorEntity = majorDao.selectById(majorId);
        Long depId = majorEntity.getDepId();
        DepEntity depEntity = depDao.selectById(depId);
        StudentAllVo studentAllVo = new StudentAllVo();
        BeanUtils.copyProperties(student, studentAllVo);
        studentAllVo.setClassName(classEntity.getName());
        studentAllVo.setMajorName(majorEntity.getName());
        studentAllVo.setDepName(depEntity.getName());

        return studentAllVo;
    }

    @Override
    public List<Long> selectIdsByClassId(Long classId) {
        QueryWrapper<StudentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("class_id", classId);
        List<StudentEntity> studentEntities = baseMapper.selectList(wrapper);
        List<Long> collect = studentEntities.stream().map(item -> item.getId()).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<StudentDetailTo> selectStudentInfoByClassId(Long classId, String studentName) {
        QueryWrapper<StudentEntity> wrapper = new QueryWrapper<>();
        if(classId != null){
            wrapper.eq("class_id", classId);
        }
        if(studentName != null && !StringUtils.isEmpty(studentName)){
            wrapper.eq("name", studentName);
        }
        List<StudentEntity> studentEntities = baseMapper.selectList(wrapper);
        List<StudentDetailTo> collect = studentEntities.stream().map(student -> {
            StudentDetailTo studentDetailTo = new StudentDetailTo();
            studentDetailTo.setId(student.getId());
            studentDetailTo.setStudentName(student.getName());
            ClassEntity classEntity = classDao.selectById(student.getClassId());
            studentDetailTo.setClassName(classEntity.getName());
            return studentDetailTo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<ClassEntity> selectClassInfoByStudentIds(List<Long> studentIds) {

        List<Long> classIds = baseMapper.selectDistinctClassIds(studentIds);
        List<ClassEntity> classEntities = classDao.selectBatchIds(classIds);
        return classEntities;
    }

}